<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%@ page language="java" import="java.text.SimpleDateFormat"%>
<%@ page import="org.mebusw.struts.davincicode.CardHand"%>
<%@ page import="org.mebusw.struts.davincicode.DavinciCard"%>
<%@ page import="org.mebusw.struts.vo.GameTable"%>
<%@ page import="org.mebusw.dao.factory.DAOFactory"%>
<%@ page import="org.mebusw.dao.IGameTableDAO"%>

<%--detect.jsp?playername=mebusw&gid=10--%>
<%

	//disable IE to cache for the same GET request
	response.setHeader("Cache-Control", "no-cache");
	response.setContentType("text/xml");

	String playerName = request.getParameter("playerName");
	String gid = request.getParameter("gid");
	//System.out.println(request.getQueryString());
	SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
			"hh:mm:ss EEEE-MMMM-dd-yyyy ");
	Date date = new Date();
	
	
	out.clear(); //make sure no any white space before "<?xml..." 
	out.print("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
	out.print("<response timer='0' version='$Rev: 50 $'>");
	out.print("<gid>" + gid + "</gid>");
	out.print("<date>" + simpleDateFormat.format(date.getTime())
			+ "</date>");

	///////////////////////////////////////////////////////
    IGameTableDAO dao = DAOFactory.getIGameTableDAOInstance();
    GameTable gt = dao.findById(Integer.parseInt(gid));

    out.print("<currPlayerName>" + gt.getPlayers()[gt.getCurrPlayer()] + "</currPlayerName>");
    int localPlayerId = gt.getPlayerId(playerName);
    out.print("<localPlayerId>" + localPlayerId + "</localPlayerId>");
    out.print("<description>" + gt.getDescription() + "</description>");
    
    out.print("<takeDisable>" + "false" + "</takeDisable>");
    out.print("<guessDisable>" + "false" + "</guessDisable>");
    out.print("<finishDisable>" + "true" + "</finishDisable>");

    out.print("<players count='99'>");
    for (int i = 0; i < gt.getPlayerCount(); i++) {
        out.print(String.format("<player playerId='%d' playerName='%s' status='%s'>",
                i, gt.getPlayers()[i], gt.getPlayerStates()[i]));
    	CardHand ch = gt.getCardHands()[i];
        DavinciCard card;
        for(int j = 0; j < ch.size(); j++)  {
            card = ch.get(j);
            String value = String.valueOf(card.getValue());
            String cardId = String.valueOf(card.getCardId());
            String color = "";
            if (localPlayerId != i && ! card.isUncover())   {
               //all of other player's standing cards should be invisible
               value = "?";
            }
            if (card.isBlack())   {
                color = "black";
            } else {
            	color = "white";
            }
            out.print(String.format("<card value='%s' cardId='%s' color='%s'>", 
                    value, cardId, color));
            out.print("</card>");

        }
    	
        out.print("</player>");
    }
    out.print("</players>");
    
    
    ///////////////////////////////////////////
    out.print("</response>");

	
%>

